import { Button } from 'cc';
import { Label } from 'cc';
import { _decorator, Component, Node } from 'cc';
import UtilTool from '../../tools/UtilTool';
import { PD } from '../../player/PlayerData';
import { tween } from 'cc';
import AbstractComponent from '../../base/AbstractComponent';
import { App } from '../../App';
import { tweenNumber } from '../../../../extensions/build_game_auto/@types/packages/scene/@types/cce/3d/manager/camera/tween';
import { Tween } from 'cc';
import { loadCommonPreFab, showErrorTip, showTip } from '../../tools/PopuUp';
import wsTool from '../../net/ws';
import CommConst from '../../const/CommConst';
import { EventConst } from '../../common/Common';
import { Sprite } from 'cc';
const { ccclass, property } = _decorator;

let DOWN_TOTAL_TIME = 60; // 倒计时总时间

/**
 * 血流匹配界面
 */
@ccclass('BloodMatch')
export class BloodMatch extends AbstractComponent {

    @property(Button)
    btn_cancel : Button = null

    @property(Button)
    btn_success : Button = null

    @property(Node)
    sp_match1 : Node = null

    @property(Node)
    sp_match2 : Node = null

    @property(Node)
    headMask : Node = null

    @property(Node)
    sp_matchWho : Node = null

    @property(Node)
    sp_matchWho2 : Node = null

    @property(Node)
    sp_matchWho3 : Node = null

    @property(Node)
    sp_matchWho4 : Node = null

    @property(Label) 
    label_mine : Label = null

    @property(Label) 
    label_match : Label = null

    bloodLevel = 0; // 流血等级 0初级场 1高级场

    sendMsgTween : any = null;

    isMatchSuccess = false; // 是否匹配成功

    downTimeCount = DOWN_TOTAL_TIME // 倒计时

    startRunHead = false    // 开始滚动头像

    start() {

        this.setMatchSuccessState(false)
        
        // 发送匹配消息
        this.sendMatchingMsg()
        this.updateHead(this.sp_match1, PD.user.headId)

        // 开始倒计时
        this.schedule(this.onMatchCountDown, 1, DOWN_TOTAL_TIME, 0)

        // 0.5秒后开始滚动匹配的头像
        setTimeout(() => {
            this.startRunHead = true
        }, 500);

        // 匹配中动画
        // UtilTool.showTextWaitAction(this.label_match, App.language.getLanguageLab(2009))

        this.label_match.string = App.language.getLanguageLab(2009) + UtilTool.formatStr("(%s)", this.downTimeCount)
        this.label_mine.string = UtilTool.formatNickOrAddress({"nickname":PD.user.nickname, "address":PD.user.addressUrl, "addressLen":6})
        this.sp_matchWho.setPosition(0, 0)
        this.sp_matchWho2.setPosition(0, -268)
        this.sp_matchWho3.setPosition(0, -536)
        this.sp_matchWho4.setPosition(0, -804)

        App.Event.on(EventConst.BLOOD_MATCH_CUC, this.onMatchSuccess, this) // 匹配成功
    }

    update(deltaTime: number) {
        if (this.startRunHead && !this.isMatchSuccess) {
            let y1 = this.sp_matchWho.getPosition().y + 10
            let y2 = this.sp_matchWho2.getPosition().y + 10
            let y3 = this.sp_matchWho3.getPosition().y + 10
            let y4 = this.sp_matchWho4.getPosition().y + 10
            if (y1>268){
                y1 = -804
            }
            if (y2>268){
                y2 = -804
            }
            if (y3>268){
                y3 = -804
            }
            if (y4>268){
                y4 = -804
            }

            this.sp_matchWho.setPosition(0, y1)
            this.sp_matchWho2.setPosition(0, y2)
            this.sp_matchWho3.setPosition(0, y3)
            this.sp_matchWho4.setPosition(0, y4)
        }
    }

    // 更新头像
    updateHead(headNode, headId) {
        let headDict = App.Scene.getHallRes("images/head")
        if (headDict["head_" + headId]) {
            headNode.getComponent(Sprite).spriteFrame = headDict["head_" + headId]
        }
    }

    initData(bloodLevel) {
        this.bloodLevel = bloodLevel
    }

    // 发送匹配消息
    sendMatchingMsg() {
        wsTool.send({msgCode:CommConst.Req_BLOOD_MATCH,"bloodLevel": this.bloodLevel})
        .then((data) => {
            if(data.result == 0) {
              
            } else {
                showErrorTip(data.result)
            }
        })
    }

    // 匹配成功后设置状态
    setMatchSuccessState(isSuccess) {
        this.btn_cancel.node.active = !isSuccess
        this.btn_success.node.active = isSuccess
        this.headMask.active = !isSuccess
        this.sp_match2.active = isSuccess
    }

    // 匹配成功
    onMatchSuccess(data) {

        this.isMatchSuccess = true

        Tween.stopAllByTarget(this.node)

        // 设置房主
        this.unschedule(this.onMatchCountDown)
        this.label_match.string = UtilTool.formatNickOrAddress({"nickname":data.nickName, "address":data.account, "addressLen":6})
        this.updateHead(this.sp_match2, data.headId)
        this.setMatchSuccessState(true)
    }

    // 匹配倒计时
    onMatchCountDown() {

        this.downTimeCount -= 1

        if(this.downTimeCount < 0) {
            this.unschedule(this.onMatchCountDown)
            this.downTimeCount = 0
        }

        this.label_match.string = App.language.getLanguageLab(2009) + UtilTool.formatStr("(%s)", this.downTimeCount)

    }

    // 匹配失败
    onMatchFail() {
        UtilTool.printLog("test onMatchingFail")
        this.startRunHead = false
        showTip(App.language.getLanguageLab(2011))

        tween(this.node).delay(1).call(() => {
            this.close()
        }).start()

    }
    
    // 取消匹配
    onBtnCancel() {
        wsTool.send({msgCode:CommConst.Req_BLOOD_NOTMATCH})
        .then((data) => {
            if(data.result == 0) {
                this.close()
            } else {
                showErrorTip(data.result)
            }
        })
    }

    protected onDestroy(): void {
        App.Event.off(EventConst.BLOOD_MATCH_CUC, this.onMatchSuccess, this)
    }

}

